<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Account extends MY_Controller {

	private $MY_VAR = "VASILICA";
	
	function __construct()
	{
		parent::__construct();
		$this->load->library(array('session', 'account_func'));

		$user_type = $this->session->userdata("USER_TYPE");
		switch ($user_type) {
			case "EW":{
				$this->load->model("ewallet_model", "model", TRUE);
				break;
			}

			case "IP":{
				$this->load->model("payments_model", "model", TRUE);
				break;
			}
			
			default:{
				$this->load->model("ewallet_model", "model", TRUE);
				break;
			}
		}
		$wallets =  $this->getWalletsData();
		$currencies = $this->model->get_currencies();
		$this->template->set("wallets", $wallets);
		$this->template->set("av_currencies", $currencies);
		$balance = $this->calc_balance();
		if($balance){
			$cID = $balance["FCODE"];
			$res = $this->model->get_currency($cID);
			$cSign = $res['C_SIGN'];
			$balance_ammount = $balance["BALANCE"];
		}
		else{
			$balance_ammount = 0;	
			$cSign = "$";
		}
		$this->template->set("balance", $balance_ammount);
		$this->template->set("cSign", $cSign);
		
	}

	function index(){
		$this->template->set("title", "My Account");
		$data['logged'] = $this->logged;
		$data['submenus'] = $this->sub_menus;
		$this->template->load("user/layout_account", "user/account_view", $data);
	}
	
	function operations() {
		$this->template->set("title", "My Account");
		$this->template->set("section", "Operations");
		$this->template->set("subSection", $this->account_func->subSectionName($this->uri->segment(4)));
		$data['logged'] = $this->logged;
		$data['submenus'] = $this->sub_menus;
		//$path = FCPATH . "application/controllers/user/acc_operations.php";
		//print $path;
		//'$this->load->file($path);
		
		$action = $this->uri->segment(4);
		switch ($action) {
			case "deposit":{
				$this->deposit_view($data);
				break;	
			}
			
			case "withdrawal":{
				$this->operations_view($data);
				break;	
			}
			
			case "send-money":{
				$this->operations_view($data);
				break;	
			}
			
			case "mass-payment":{
				$this->operations_view($data);
				break;	
			}
			
			case "request-money":{
				$this->operations_view($data);
				break;	
			}
			
			case "internal-transfer":{
				$segment5 = $this->uri->segment(5);
				switch ($segment5) {
					case "save":{
						$this->validateInternalTransfer();
						break;
					}
					
					case "confirm": {
						$this->internal_transfer_confirm_view($data);
						break;
					} 
					
					case "confirmSave": {
						$this->saveInternalTransferConfirm();
						break;
					}
					
					default:{
						$this->internal_transfer_view($data);
						break;
					}
				}
				break;	
			}
			
			case "exchange":{
				$segment5 = $this->uri->segment(5);
				switch ($segment5) {
					case "save":{
						$this->validateExchange();
						break;
					}
					
					case "confirm": {
						$this->exchange_confirm_view($data);
						break;
					} 
					
					case "confirmSave": {
						$this->saveExchangeConfirm();
						break;
					} 
					
					default:{
						$this->exchange_view($data);
						break;
					}
				}
				//$this->operations_view($data);
				break;	
			}
			
			default:{
				$this->operations_view($data);
				break;
			}
		}
	}
	
	function thistory() {
		$this->template->set("title", "My Account");
		$this->template->set("section", "Transaction History");
		$this->template->set("subSection", $this->account_func->subSectionName($this->uri->segment(4)));
		$data['logged'] = $this->logged;
		$data['submenus'] = $this->sub_menus;
		$this->template->load("user/layout_account", "user/account_view", $data);
	}
	
	function reports() {
		$this->template->set("title", "My Account");
		$this->template->set("section", "Reports");
		$this->template->set("subSection", $this->account_func->subSectionName($this->uri->segment(4)));
		$data['logged'] = $this->logged;
		$data['submenus'] = $this->sub_menus;
		$this->template->load("user/layout_account", "user/account_view", $data);
	}
	
	function profile() {
		$this->template->set("title", "My Account");
		$this->template->set("section", "Profile");
		$this->template->set("subSection", $this->account_func->subSectionName($this->uri->segment(4)));
		$data['logged'] = $this->logged;
		$data['submenus'] = $this->sub_menus;
		
		$segment4 = $this->uri->segment(4);
		switch ($segment4) {
			case "wallets":{
				$action = $this->uri->segment(5);
				switch ($action) {
					case "add":{
						$this->addWallet();
						break;
					}
					
					default:{
						$this->wallet_view($data);
						break;
					}
				}
				break;
			}
			case "wallet" : {
				$data['walletCode'] = $this->uri->segment(5);
				$action = $this->uri->segment(6);
				switch ($action) {
					case "save": {
						$this->saveWallet($data['walletCode']);
						break;
					}

					case "addCurrency": {
						$this->addCurrency($data['walletCode']);
						break;
					}
					
					case "removeCurr": {
						$this->removeCurrency($data['walletCode']);
						break;
					}
					
					default:{
						$this->wallet_properties_view($data);
						break;
					}
				}
				break;
			}

			case "change_balance": {
				$this->change_balance();
				break;
			}
			
			case "switch_view": {
				
				break;
			}
			default:{
				$this->default_view($data);
				break;
			}
		}		
	}
	
	
	/*start of view functions section*/
	private function default_view($data) {
		$this->template->load("user/layout_account", "user/account_view", $data);
	}
	
	private function operations_view($data) {
		$this->template->load("user/layout_account", "user/operations_view", $data);
	}
	
	private function deposit_view($data) {
		$this->template->load("user/layout_account", "user/deposit_view", $data);
	}
	
	private function exchange_view($data) {
		$userID = $this->session->userdata("ID");
		$wallet_list = $this->model->get_purses($userID);
		$data["wallet_list"] = $wallet_list ? $wallet_list : array();
		$currencies = $this->model->get_currencies();   
		$data["currencies"] = $currencies ? $currencies : array();
		
		$this->load->database('default', TRUE);
		$data["value_ammount"] = $this->session->userdata("ammount");
		$data["from_id"] = $this->session->userdata("convertFrom");
		$data["to_id"] = $this->session->userdata("convertTo");
		$data["exType"] = $this->session->userdata("exchangeType");
		$vars = array('ammount' => '', 'convertFrom' => '', 'convertTo' => '', 'exchangeType' => '');
		$this->session->unset_userdata($vars);   
		$this->template->load("user/layout_account", "user/exchange_view", $data);
	}
	
	private function exchange_confirm_view($data) {
		$res = $this->calc_convert();

		if($res['validCode']){
			$data['wallet'] = $res['wallet'];
			$data['currency'] = $res['currency'];
			$data['to_currency'] = $res['to_currency'];
			$data['ex_rate'] = $res['ex_rate'];
			$data['ex_ammount'] = $res['ex_ammount'];
			$data['ammount'] = $res['ammount'];

			$this->template->load("user/layout_account", "user/exchange_confirm_view", $data);
		}
		else{
			redirect(site_url("user/account/operations/exchange"));
		}
	}
	
	private function internal_transfer_view($data) {
		
		$data["value_ammount"] = $this->session->userdata("ammount");
		$data["from_id"] = $this->session->userdata("accountFrom");
		$data["to_id"] = $this->session->userdata("accountTo");
		$userID = $this->session->userdata("ID");
		$wallet_list = $this->model->get_purses($userID);
		$data["wallet_list"] = $wallet_list ? $wallet_list : array();
		$wallets = $this->model->get_wallets($userID);   
		$data['comment'] = $this->session->userdata("comment");   
		$data["wallets"] = $wallets ? $wallets : array();   
		$this->template->load("user/layout_account", "user/internal_transfer_view", $data);
	}
	
	private function internal_transfer_confirm_view($data) {
		$this->load->database("default");
		$data["value_ammount"] = $this->session->userdata("ammount");
		$data["to_id"] = $this->session->userdata("accountTo");
		$data["comment"] = $this->session->userdata("comment");
		$from_id_raw = $this->session->userdata("accountFrom");
		
		$arr_wallet_from = explode("-", $from_id_raw);
		if(count($arr_wallet_from) == 2){
			$from_id = $arr_wallet_from[0];
			$from_wa_id = $arr_wallet_from[1];
			$wallet1 = $this->model->get_wallet($this->session->userdata("ID"), NULL, $from_id);
			$wallet2 = $this->model->get_wallet($this->session->userdata("ID"), NULL, $data["to_id"]);
			$currency = $this->model->get_wcurrencies($from_id, $from_wa_id);
			$data['wallet_from'] = $wallet1;
			$data['wallet_to'] = $wallet2;
			$data['currency'] = $currency;
			$this->template->load("user/layout_account", "user/internal_transfer_confirm_view", $data);
		}
		else{
			redirect(site_url("user/account/operations/internal-transfer"));
		}
		
	}
	
	private function wallet_view($data) {
		$wallets =  $this->getWalletsData();
		$data['wallets'] = $wallets;
		$data['av_currencies'] = $this->model->get_currencies();//available currencies
		
		$this->template->load("user/layout_account", "user/wallets_view", $data);
	}
	
	private function wallet_properties_view($data) {
		$wallet = $this->getWalletData($data['walletCode']);
		if($wallet){
			$av_currencies = $this->model->get_currencies();//available currencies
			$data['wallet'] = $wallet;
			$wall_currencies = array();
			$arr_av = array();
			$arr_curr = array();
			foreach ($av_currencies as $value) {
				$arr_av[$value['ID']] = $value['C_SYMBOL'];
			}
			foreach ($wallet['currencies'] as $value) {
				$arr_curr[$value['ID']] = $value['C_SYMBOL'];
			}
			$data['av_currencies'] = array_diff($arr_av, $arr_curr);
			$this->template->load("user/layout_account", "user/wallet_properties_view", $data);
		}
		else{
			redirect(site_url("user/account/profile/wallets"));
		}
	}
	/*end of view functions*/
	
	
	
	
	private function change_balance() {
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('ddlCurrencies', 'Currency', 'required');
		if ($this->form_validation->run() == FALSE){
			$data['error'] = strip_tags(validation_errors());
       	}
       	else{
       		$res = $this->model->change_balance((int)$this->session->userdata("ID"), (int)$this->input->post("ddlCurrencies"));
       		if($res){
       		 	$data['refresh'] = true; 
       		 	//$data['delay'] = 1;
       		}
       		else{
       			 $data['error'] = "The balance currency couldn't be changed";
       		}	
       	}
		echo json_encode($data);
	}
	
	private function calc_balance() {
		$balance = array("FCODE" => 5, "BALANCE" => 0);
		$balance = $this->model->get_balance($this->session->userdata("ID"));
		
		return $balance;
	}
	
	private function switch_wallets_view() {
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('enable', 'enable', 'required');
		if ($this->form_validation->run() == FALSE){
			$data['error'] = strip_tags(validation_errors());
       	}
       	else{
       		
       	}
       	echo json_encode($data);
	}
	
	
	/**
	 * 
	 * Returns the specific data for the user current wallet
	 * @return array with wallet data  
	 */
	protected function getWalletData($walletCode) {
		$user_id = $this->session->userdata("ID");
		$wallet = $this->model->get_wallet($user_id, $walletCode);
		if($wallet){
			$wallet['currencies'] = $this->model->get_wcurrencies($wallet['ID']);
			return $wallet;
		}
		else{
			return false;
		}
	}
	
	protected function getWalletsData() {
		$user_id = $this->session->userdata("ID");
		$wallets = $this->model->get_wallets($user_id);
		$res_wallets = array();
		foreach ($wallets as $key => $val) {
			foreach ($wallets[$key] as $key1 => $val1) {
				$res_wallets[$key][$key1] = $val1;
			}
			$currencies = $this->model->get_wcurrencies($res_wallets[$key]['ID']);
			$res_wallets[$key]['currencies'] = $currencies;
		}
		
		return $res_wallets;
	}

	private function saveWallet($walletCode) {
	$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('txtDescription', 'Title', 'required|min_length[2]|max_length[30]|alpha_dash');
		if ($this->form_validation->run() == FALSE){
			$data['error'] = strip_tags(validation_errors());
       	}
       	else{
       		if(strlen($walletCode) == 11){
       			$res = $this->model->save_wallet_title($walletCode, $this->session->userdata("ID"), $this->input->post("txtDescription"));
       			$res ? $data['refresh'] = true : $data['error'] = "The wallet info couldn't be updated";
       		}
       		else{
       			$data['error'] = "Wrong wallet code";
       		}
       	}
		echo json_encode($data);
	} 
	
	private function addCurrency($walletCode) {
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('ddlCurrencies', 'Currency', 'required');
		if ($this->form_validation->run() == FALSE){
			$data['error'] = strip_tags(validation_errors());
       }
       else{
       		$user_id = $this->session->userdata("ID");
       		
       		$w_currency = new Wallet_currency();
       		$wallet = $this->model->get_wallet($user_id, $walletCode);
       		$w_currency->WALLET_ID = $wallet['ID'];
       		if($w_currency->WALLET_ID && strlen($walletCode) == 11){
       			$w_currency->AMMOUNT = 0;
       			$w_currency->CURRENCY_ID = $this->input->post("ddlCurrencies");
       			if($this->model->save_wallet_currency($w_currency)){
	       			$data['refresh'] = true;
		       		$data['delay'] = 1;//TODO NEEDS TO BE CHANGED
       			}
       			else{
       				$data['error'] = "Currency could not be added";
       			}
       		}
       		else{
       			$data['error'] = "Currency could not be added";
       		}
       }
       echo json_encode($data);
	}
	
	private function removeCurrency($walletCode) {
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('currID', 'currID', 'required');
		if ($this->form_validation->run() == FALSE){
			$data['error'] = strip_tags(validation_errors());
       }
       else{
       		$user_id = $this->session->userdata("ID");
       		
       		$w_currency = new Wallet_currency();
       		$wallet = $this->model->get_wallet($user_id, $walletCode);
       		$w_currency->WALLET_ID = $wallet['ID'];
       		if($w_currency->WALLET_ID && strlen($walletCode) == 11){
       			$arr = explode("-", $this->input->post("currID"));
       			if(count($arr) == 2){
       				if($w_currency->WALLET_ID == $arr[0]){
		       			$w_currency->CURRENCY_ID = $arr[1];
		       			$w_data["WALLET_ID"] = $w_currency->WALLET_ID; 
		       			$w_data["CURRENCY_ID"] = $arr[1]; 
		       			if($this->model->remove_wallet_currency($w_data)){
			       			$data['refresh'] = true;
				       		$data['delay'] = 1;//TODO NEEDS TO BE CHANGED
		       			}
		       			else{
		       				$data['error'] = "Currency could not be removed";
		       			}
       				}
	       			else{
	       				$data['error'] = "Currency could not be removed";
	       			}
       			}
       			else{
       				$data['error'] = "Currency could not be removed";
       			}

       		}
       		else{
       			$data['error'] = "Currency could not be removed";
       		}
       		unset($w_currency);
       }
       echo json_encode($data);
	}
	
	
	private function addWallet() {
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('txtDescription', 'Title', 'required|min_length[2]|max_length[30]|alpha_dash');
		$this->form_validation->set_rules('ddlCurrencies', 'Currency', 'required');
		if ($this->form_validation->run() == FALSE){
			$data['error'] = strip_tags(validation_errors());
       }
       else{
       	
       		$wallet = new Wallet();
       		$wallet->USER_ID = $this->session->userdata("ID");
       		$wallet->DATE_OPENED = date("Y-m-d H:i:s");
       		$wallet->NAME = $this->input->post("txtDescription");
       		$wallet->WALLET_CODE = $this->generate_wallet_code();
       		
       		$w_currency = new Wallet_currency();
       		$w_currency->WALLET_ID = $this->model->save_wallet($wallet);
       		if($w_currency->WALLET_ID){
       			$w_currency->AMMOUNT = 0;
       			$w_currency->CURRENCY_ID = $this->input->post("ddlCurrencies");
       			$this->model->save_wallet_currency($w_currency);

       			$data['refresh'] = true;
	       		$data['delay'] = 1;//TODO NEEDS TO BE CHANGED
       		}
       		else{
       			$data['error'] = "Wallet could not be saved";
       		}
       }
       echo json_encode($data);
	}
	
	
	private function validateExchange() {
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('convertFrom', 'Source of Founds', 'required|callback__check_convert_from_wallet');
		$this->form_validation->set_rules('convertTo', 'Convert To', 'required');
		$this->form_validation->set_rules('ammount', 'Ammount', 'required|callback__check_ammount');
		$this->form_validation->set_rules('exType', 'Exchange Type', 'required|numeric');
		if ($this->form_validation->run() == FALSE){
			$data['error']['convertFrom'] = strip_tags(form_error('convertFrom'));
			$data['error']['convertTo'] = strip_tags(form_error('convertTo'));
			$data['error']['ammount'] = strip_tags(form_error('ammount'));
			$data['error']['message'] = "Some information is incorrect or missing. Please correct your entries and try again.";
			$data['link'] = site_url("user/account/operations/exchange#message");
       }
       else{
       	$this->load->database('default', TRUE, TRUE);
       	$arr = array('convertFrom' => '', 'convertTo' => '', 'ammount' => '');
       	$this->session->unset_userdata($arr);
       	$this->session->set_userdata("convertFrom", $this->input->post("convertFrom"));
       	$this->session->set_userdata("convertTo", $this->input->post("convertTo"));
       	$this->session->set_userdata("ammount", $this->input->post("ammount"));
       	$this->session->set_userdata("exchangeType", $this->input->post("exType"));
       	$data['redirect'] = site_url("user/account/operations/exchange/confirm");
       }
       echo json_encode($data);
	}
	
	private function saveExchangeConfirm() {
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('submitForm', 'submitForm', 'required|callback__check_conversion');
		if($this->form_validation->run() == FALSE) {
			$data['error']['message'] = strip_tags(validation_errors());
			$data['link'] = site_url("user/account/operations/exchange/confirm#message");
			$data['redirect'] = site_url("user/account/operations/exchange");
			$data['delay'] = 3;
		}
		else {
			$this->load->database('default', TRUE, TRUE);
	       	$arr = array('convertFrom' => '', 'convertTo' => '', 'ammount' => '');
	       	$this->session->unset_userdata($arr);
	       	$data['success'] = "Money converted successfuly";
	       	$data['link'] = site_url("user/account/operations/exchange/confirm#message");
	       	$data['redirect'] = site_url("user/account/operations/exchange");
	       	$data['delay'] = 5;
       }
       echo json_encode($data);
	}
	
	private function validateInternalTransfer() {
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('accountFrom', 'From', 'required|callback__check_from_wallet');
		$this->form_validation->set_rules('accountTo', 'To', 'required');
		$this->form_validation->set_rules('ammount', 'Ammount', 'required|callback__check_ammount');
		if ($this->form_validation->run() == FALSE){
			$data['error']['accountFrom'] = strip_tags(form_error('accountFrom'));
			$data['error']['accountTo'] = strip_tags(form_error('accountTo'));
			$data['error']['ammount'] = strip_tags(form_error('ammount'));
			$data['error']['message'] = "Some information is incorrect or missing. Please correct your entries and try again.";
			$data['link'] = site_url("user/account/operations/internal-transfer#message");
       }
       else{
       	$this->load->database('default', TRUE, TRUE);
       	$arr = array('accountFrom' => '', 'accountTo' => '', 'ammount' => '');
       	$this->session->unset_userdata($arr);
       	$this->session->set_userdata("accountFrom", $this->input->post("accountFrom"));
       	$this->session->set_userdata("accountTo", $this->input->post("accountTo"));
       	$this->session->set_userdata("ammount", $this->input->post("ammount"));
       	$this->session->set_userdata("comment", $this->input->post("comment"));
       	$data['redirect'] = site_url("user/account/operations/internal-transfer/confirm");
       }
       echo json_encode($data);
	}
	
	private function saveInternalTransferConfirm() {
		$this->load->library(array('form_validation'));
		$this->form_validation->set_rules('submitForm', 'submitForm', 'required|callback__check_transfer');
		if($this->form_validation->run() == FALSE) {
			$data['error']['message'] = strip_tags(validation_errors());
			$data['link'] = site_url("user/account/operations/internal-transfer/confirm#message");
			$data['redirect'] = site_url("user/account/operations/internal-transfer");
			$data['delay'] = 3;
		}
		else {
			$this->load->database('default', TRUE, TRUE);
	       	$arr = array('accountFrom' => '', 'accountTo' => '', 'ammount' => '', 'comment' => '');
	       	$this->session->unset_userdata($arr);
	       	$data['success'] = "Money transfered successfuly";
	       	$data['link'] = site_url("user/account/operations/internal-transfer/confirm#message");
	       	$data['redirect'] = site_url("user/account/operations/internal-transfer");
	       	$data['delay'] = 5;
       }
       echo json_encode($data);
	}
	
	
	/**
	 * 
	 * Generates a unique code for a wallet
	 * @return string (wallet code)
	 */
	function generate_wallet_code() {
		$result = $this->model->get_wallets_codes();
		$excludes = array();
		foreach ($result as $key => $val) {
			$excludes[$key] = $val->WALLET_CODE;
		}
		$user_type = $this->session->userdata("USER_TYPE");
		
		$key = mt_rand(1, 999999999);
		$code = str_pad($key, 9, '0', STR_PAD_LEFT);
		$code = $user_type . $code;
		while (in_array($code, $excludes)) {
			$key = mt_rand(1, 999999999);
			$code = str_pad($key, 9, '0', STR_PAD_LEFT);
			$code = "EW" . $code;
		}
		return $code;
	}
	
	
	/*check functions region*/
	function _check_convert_from_wallet($str) {
		$arr = explode("-", $str, 3);
		if(count($arr) == 2){
			if($arr[1] == $this->input->post("convertTo")){
				$this->form_validation->set_message("_check_convert_from_wallet", "There is nothing to convert");
				return FALSE;
			}
			else{
				return TRUE;	
			}
		}
		else{
			$this->form_validation->set_message("_check_convert_from_wallet", "There is nothing to convert");
			return FALSE;
		}
		
	}
	
	function _check_from_wallet($str) {
		$arr = explode("-", $str, 3);
		if(count($arr) == 2){
			if($arr[0] == $this->input->post("accountTo")){
				$this->form_validation->set_message("_check_from_wallet", "There is nothing to transfer");
				return FALSE;
			}
			else{
				return TRUE;	
			}
		}
		else{
			$this->form_validation->set_message("accountFrom", "There is nothing to transfer");
			return FALSE;
		}
		
	}
	
	function _check_ammount($number){
		if((is_numeric($number)) && (round($number, 2) > 0)){
			return TRUE;
		}
		else{
			$err = "You have entered an invalid amount. (e.g. 5.00)";
			$this->form_validation->set_message("_check_ammount", $err);
			return FALSE;
		}
	}
	
	function _check_conversion($str){
		if((is_numeric($str)) && ($str == 1)){
			$res = $this->calc_convert();
			if($res['validCode']){
				if($this->model->save_exchange($res['wallet_id'], $res['curr_id'], $res['to_id'], $res['ammount'], $res['ex_ammount'])){
					return TRUE;
				}
				else{
					$err = "Not enough money";
					$this->form_validation->set_message("_check_conversion", $err);
					return FALSE;
				}
				
			}
			$err = "The request could not be completed";
			$this->form_validation->set_message("_check_conversion", $err);
		}
		else{
			$err = "The request could not be completed";
			$this->form_validation->set_message("_check_conversion", $err);
			return FALSE;
		}
	}
	
	function _check_transfer($str){
		if((is_numeric($str)) && ($str == 1)){
			$from_id_raw = $this->session->userdata("accountFrom");
			$to_id = $this->session->userdata("accountTo");
		
			$arr_wallet_from = explode("-", $from_id_raw);
			if(count($arr_wallet_from) == 2){
				$from_id = $arr_wallet_from[0];
				$from_wa_id = $arr_wallet_from[1];
				if($this->model->save_internal_transfer($from_id, $from_wa_id, $to_id, $this->session->userdata("ammount"))){
					return TRUE;
				}
				else{
					$err = "Not enough money";
					$this->form_validation->set_message("_check_transfer", $err);
					return FALSE;
				}
			}
			$err = "The request could not be completed";
			$this->form_validation->set_message("_check_transfer", $err);
		}
		else{
			$err = "The request could not be completed";
			$this->form_validation->set_message("_check_transfer", $err);
			return FALSE;
		}
	}
	/*end of check functions region*/
	
	/*other types of utility functions region*/
	private function calc_convert(){
		$this->load->database("default");
		$data["value_ammount"] = $this->session->userdata("ammount");
		$data["from_id"] = $this->session->userdata("convertFrom");
		$data["to_id"] = $this->session->userdata("convertTo");
		
		$exType = $this->session->userdata("exchangeType");
		$userID = $this->session->userdata("ID");

		$from_id_raw = $this->session->userdata("convertFrom");
		
		$arr_wallet_from = explode("-", $from_id_raw);
		if(count($arr_wallet_from) == 2){
			$from_id = $arr_wallet_from[0];
			$curr_id = $arr_wallet_from[1];
			$result = $this->model->get_ex_rate($curr_id, $this->session->userdata("convertTo"));
			$ex_rate = $result['RATE'];
			
			$wallet = $this->model->get_wallet($this->session->userdata("ID"), NULL, $from_id);
			$currency = $this->model->get_wcurrencies($from_id, $curr_id);
			$to_currency = $this->model->get_currency($data['to_id']);
			if($exType == "2") {
				$ex_ammount = round($data['value_ammount'] * (1/$ex_rate), 2);
			}
			else{
				$ex_ammount = round($data['value_ammount'] * $ex_rate, 2);
			}
			
			$data['wallet_id'] = $from_id;
			$data['curr_id'] = $curr_id;
			$data['wallet'] = $wallet;
			$data['currency'] = $currency;
			$data['to_currency'] = $to_currency;
			$data['ex_rate'] = $ex_rate;
			$data['ex_ammount'] = ($exType == "2") ? $data["value_ammount"] : round($ex_ammount, 2);
			$data['ammount'] = ($exType == "2") ? round($ex_ammount, 2) : $data["value_ammount"];;
			$data['exType'] = $exType;
			$data['validCode'] = true;
		}
		else{
			$data['validCode'] = false;
		}
			return $data;
	}

/*end of other types of utility functions*/
}
